{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "212be17ae01eaeb0",
   "metadata": {},
   "source": [
    "### step1：导入有关数据分析和处理的库\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b38b4a0df839169",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基本库 请勿修改\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9722e58523890494",
   "metadata": {},
   "source": [
    "### step2：导入有关sklearn中有关数据集划分，数据集检验的库\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ed02b21ee79217a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:10:44.455560Z",
     "start_time": "2024-12-30T11:10:44.451073Z"
    }
   },
   "outputs": [],
   "source": [
    "# sklearn部分功能库 请勿修改\n",
    "# 数据集划分\n",
    "from sklearn.model_selection import train_test_split\n",
    "# 数据归一化\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 模型评估\n",
    "from sklearn.metrics import r2_score\n",
    "from sklearn.metrics import mean_absolute_error\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn.metrics import mean_absolute_percentage_error"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f22246737c069cb8",
   "metadata": {},
   "source": [
    "### step3：读入数据集\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d44c1f7c-0bb1-4051-85de-ae1c4817cda5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting openpyxl\n",
      "  Using cached openpyxl-3.1.5-py2.py3-none-any.whl.metadata (2.5 kB)\n",
      "Collecting et-xmlfile (from openpyxl)\n",
      "  Using cached et_xmlfile-2.0.0-py3-none-any.whl.metadata (2.7 kB)\n",
      "Using cached openpyxl-3.1.5-py2.py3-none-any.whl (250 kB)\n",
      "Using cached et_xmlfile-2.0.0-py3-none-any.whl (18 kB)\n",
      "Installing collected packages: et-xmlfile, openpyxl\n",
      "Successfully installed et-xmlfile-2.0.0 openpyxl-3.1.5\n"
     ]
    }
   ],
   "source": [
    "!pip install openpyxl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fac7792edc710119",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T10:20:12.811787Z",
     "start_time": "2024-12-30T10:20:12.778103Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>左主机转速</th>\n",
       "      <th>机舱环境温度</th>\n",
       "      <th>左主机1#缸排气温度</th>\n",
       "      <th>左主机增压器转速</th>\n",
       "      <th>左主机进口燃油流量</th>\n",
       "      <th>左主机出口燃油流量</th>\n",
       "      <th>左主机1#缸排气温度.1</th>\n",
       "      <th>左主机2#缸排气温度</th>\n",
       "      <th>左主机3#缸排气温度</th>\n",
       "      <th>左主机4#缸排气温度</th>\n",
       "      <th>左主机5#缸排气温度</th>\n",
       "      <th>左主机6#缸排气温度</th>\n",
       "      <th>左主机7#缸排气温度</th>\n",
       "      <th>左主机8#缸排气温度</th>\n",
       "      <th>左主机增压器排气温度</th>\n",
       "      <th>左主机淡水压力</th>\n",
       "      <th>左主机淡水温度</th>\n",
       "      <th>左主机海水压力</th>\n",
       "      <th>左主机滑油压力</th>\n",
       "      <th>左主机滑油温度</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.481775</td>\n",
       "      <td>0.4390</td>\n",
       "      <td>0.371450</td>\n",
       "      <td>0.130208</td>\n",
       "      <td>0.001254</td>\n",
       "      <td>0.001096</td>\n",
       "      <td>0.371450</td>\n",
       "      <td>0.343125</td>\n",
       "      <td>0.345062</td>\n",
       "      <td>0.347275</td>\n",
       "      <td>0.339487</td>\n",
       "      <td>0.366200</td>\n",
       "      <td>0.400250</td>\n",
       "      <td>0.394813</td>\n",
       "      <td>0.445288</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.8574</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.033</td>\n",
       "      <td>0.6623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.465275</td>\n",
       "      <td>0.4388</td>\n",
       "      <td>0.369050</td>\n",
       "      <td>0.128942</td>\n",
       "      <td>0.001217</td>\n",
       "      <td>0.001125</td>\n",
       "      <td>0.369050</td>\n",
       "      <td>0.340275</td>\n",
       "      <td>0.343162</td>\n",
       "      <td>0.344800</td>\n",
       "      <td>0.336987</td>\n",
       "      <td>0.363000</td>\n",
       "      <td>0.397688</td>\n",
       "      <td>0.391263</td>\n",
       "      <td>0.444812</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.8573</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.033</td>\n",
       "      <td>0.6625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.485350</td>\n",
       "      <td>0.4393</td>\n",
       "      <td>0.366075</td>\n",
       "      <td>0.128617</td>\n",
       "      <td>0.001254</td>\n",
       "      <td>0.001096</td>\n",
       "      <td>0.366075</td>\n",
       "      <td>0.337713</td>\n",
       "      <td>0.341413</td>\n",
       "      <td>0.342012</td>\n",
       "      <td>0.334713</td>\n",
       "      <td>0.360063</td>\n",
       "      <td>0.395000</td>\n",
       "      <td>0.387788</td>\n",
       "      <td>0.444113</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.8573</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.034</td>\n",
       "      <td>0.6625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.467175</td>\n",
       "      <td>0.4390</td>\n",
       "      <td>0.363225</td>\n",
       "      <td>0.126953</td>\n",
       "      <td>0.001188</td>\n",
       "      <td>0.001067</td>\n",
       "      <td>0.363225</td>\n",
       "      <td>0.335375</td>\n",
       "      <td>0.340250</td>\n",
       "      <td>0.339587</td>\n",
       "      <td>0.332487</td>\n",
       "      <td>0.357525</td>\n",
       "      <td>0.392625</td>\n",
       "      <td>0.385213</td>\n",
       "      <td>0.443550</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.8573</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.033</td>\n",
       "      <td>0.6626</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.474712</td>\n",
       "      <td>0.4391</td>\n",
       "      <td>0.361325</td>\n",
       "      <td>0.128400</td>\n",
       "      <td>0.001254</td>\n",
       "      <td>0.001125</td>\n",
       "      <td>0.361325</td>\n",
       "      <td>0.333162</td>\n",
       "      <td>0.338975</td>\n",
       "      <td>0.337462</td>\n",
       "      <td>0.330587</td>\n",
       "      <td>0.355088</td>\n",
       "      <td>0.391263</td>\n",
       "      <td>0.382050</td>\n",
       "      <td>0.442975</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.8576</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.034</td>\n",
       "      <td>0.6624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9916</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.4082</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9917</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.4085</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9918</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.4085</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9919</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.4087</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9920</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.4082</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9921 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         左主机转速  机舱环境温度  左主机1#缸排气温度  左主机增压器转速  左主机进口燃油流量  左主机出口燃油流量  \\\n",
       "0     0.481775  0.4390    0.371450  0.130208   0.001254   0.001096   \n",
       "1     0.465275  0.4388    0.369050  0.128942   0.001217   0.001125   \n",
       "2     0.485350  0.4393    0.366075  0.128617   0.001254   0.001096   \n",
       "3     0.467175  0.4390    0.363225  0.126953   0.001188   0.001067   \n",
       "4     0.474712  0.4391    0.361325  0.128400   0.001254   0.001125   \n",
       "...        ...     ...         ...       ...        ...        ...   \n",
       "9916  0.000000  0.4082    0.000000  0.000000   0.000000   0.000000   \n",
       "9917  0.000000  0.4085    0.000000  0.000000   0.000000   0.000000   \n",
       "9918  0.000000  0.4085    0.000000  0.000000   0.000000   0.000000   \n",
       "9919  0.000000  0.4087    0.000000  0.000000   0.000000   0.000000   \n",
       "9920  0.000000  0.4082    0.000000  0.000000   0.000000   0.000000   \n",
       "\n",
       "      左主机1#缸排气温度.1  左主机2#缸排气温度  左主机3#缸排气温度  左主机4#缸排气温度  左主机5#缸排气温度  \\\n",
       "0         0.371450    0.343125    0.345062    0.347275    0.339487   \n",
       "1         0.369050    0.340275    0.343162    0.344800    0.336987   \n",
       "2         0.366075    0.337713    0.341413    0.342012    0.334713   \n",
       "3         0.363225    0.335375    0.340250    0.339587    0.332487   \n",
       "4         0.361325    0.333162    0.338975    0.337462    0.330587   \n",
       "...            ...         ...         ...         ...         ...   \n",
       "9916      0.000000    0.000000    0.000000    0.000000    0.000000   \n",
       "9917      0.000000    0.000000    0.000000    0.000000    0.000000   \n",
       "9918      0.000000    0.000000    0.000000    0.000000    0.000000   \n",
       "9919      0.000000    0.000000    0.000000    0.000000    0.000000   \n",
       "9920      0.000000    0.000000    0.000000    0.000000    0.000000   \n",
       "\n",
       "      左主机6#缸排气温度  左主机7#缸排气温度  左主机8#缸排气温度  左主机增压器排气温度  左主机淡水压力  左主机淡水温度  \\\n",
       "0       0.366200    0.400250    0.394813    0.445288     0.01   0.8574   \n",
       "1       0.363000    0.397688    0.391263    0.444812     0.01   0.8573   \n",
       "2       0.360063    0.395000    0.387788    0.444113     0.01   0.8573   \n",
       "3       0.357525    0.392625    0.385213    0.443550     0.01   0.8573   \n",
       "4       0.355088    0.391263    0.382050    0.442975     0.01   0.8576   \n",
       "...          ...         ...         ...         ...      ...      ...   \n",
       "9916    0.000000    0.000000    0.000000    0.000000     0.00   0.0000   \n",
       "9917    0.000000    0.000000    0.000000    0.000000     0.00   0.0000   \n",
       "9918    0.000000    0.000000    0.000000    0.000000     0.00   0.0000   \n",
       "9919    0.000000    0.000000    0.000000    0.000000     0.00   0.0000   \n",
       "9920    0.000000    0.000000    0.000000    0.000000     0.00   0.0000   \n",
       "\n",
       "      左主机海水压力  左主机滑油压力  左主机滑油温度  \n",
       "0        0.01    0.033   0.6623  \n",
       "1        0.01    0.033   0.6625  \n",
       "2        0.01    0.034   0.6625  \n",
       "3        0.01    0.033   0.6626  \n",
       "4        0.01    0.034   0.6624  \n",
       "...       ...      ...      ...  \n",
       "9916     0.00    0.000   0.0000  \n",
       "9917     0.00    0.000   0.0000  \n",
       "9918     0.00    0.000   0.0000  \n",
       "9919     0.00    0.000   0.0000  \n",
       "9920     0.00    0.000   0.0000  \n",
       "\n",
       "[9921 rows x 20 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_excel('data.xlsx')\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "704874e4a244d591",
   "metadata": {},
   "source": [
    "### step4：数据预处理\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "591f1bcc98939696",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T10:22:49.515957Z",
     "start_time": "2024-12-30T10:22:49.505901Z"
    }
   },
   "outputs": [],
   "source": [
    "X_label = data.columns[:3]\n",
    "y_label = data.columns[3:]\n",
    "X = data.iloc[:, :3].values\n",
    "Y = data.iloc[:, 3:].values\n",
    "\n",
    "# 数据集中的数据应该是已经归一化过的，所以这里不需要再进行归一化操作\n",
    "# scaler = StandardScaler()\n",
    "# X = scaler.fit_transform(X)\n",
    "# Y = scaler.fit_transform(Y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d9e46029477d071",
   "metadata": {},
   "source": [
    "### step5：划分训练集和测试集\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9bc6a94266a9e0bb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:12:49.781704Z",
     "start_time": "2024-12-30T11:12:49.774003Z"
    }
   },
   "outputs": [],
   "source": [
    "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd615b75e9c67402",
   "metadata": {},
   "source": [
    "### step6：构建集成学习模型\n",
    "这里以ridge - 随机森林 - adaboost -- 线性回归（元模型）为例，你可以构建你想要使用的集成学习模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aacaf4ae9e7f8c57",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:03:34.009233Z",
     "start_time": "2024-12-30T11:03:33.262856Z"
    }
   },
   "outputs": [],
   "source": [
    "# ridge\n",
    "from sklearn.linear_model import Ridge\n",
    "# rf\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "# adaboost\n",
    "from sklearn.ensemble import AdaBoostRegressor\n",
    "# meta model\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.ensemble import StackingRegressor\n",
    "from sklearn.multioutput import MultiOutputRegressor\n",
    "\n",
    "estimators = [\n",
    "    ('ridge', Ridge(alpha=1.0)),\n",
    "    ('rf', RandomForestRegressor(random_state=42)),\n",
    "    ('adaboost', AdaBoostRegressor(random_state=42))\n",
    "]\n",
    "\n",
    "model = StackingRegressor(\n",
    "    estimators=estimators,\n",
    "    final_estimator=LinearRegression()\n",
    ")\n",
    "\n",
    "multi_output_model = MultiOutputRegressor(model)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3aa18477459fad7",
   "metadata": {},
   "source": [
    "### step7：训练模型\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e1f79cc44d4ac59e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:13:04.297290Z",
     "start_time": "2024-12-30T11:13:04.225485Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: black;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-1 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-1 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-1 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-1 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-1 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 1ex;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-1 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>MultiOutputRegressor(estimator=StackingRegressor(estimators=[(&#x27;ridge&#x27;, Ridge()),\n",
       "                                                             (&#x27;rf&#x27;,\n",
       "                                                              RandomForestRegressor(random_state=42)),\n",
       "                                                             (&#x27;adaboost&#x27;,\n",
       "                                                              AdaBoostRegressor(random_state=42))],\n",
       "                                                 final_estimator=LinearRegression()))</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" ><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;&nbsp;MultiOutputRegressor<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.5/modules/generated/sklearn.multioutput.MultiOutputRegressor.html\">?<span>Documentation for MultiOutputRegressor</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div class=\"sk-toggleable__content fitted\"><pre>MultiOutputRegressor(estimator=StackingRegressor(estimators=[(&#x27;ridge&#x27;, Ridge()),\n",
       "                                                             (&#x27;rf&#x27;,\n",
       "                                                              RandomForestRegressor(random_state=42)),\n",
       "                                                             (&#x27;adaboost&#x27;,\n",
       "                                                              AdaBoostRegressor(random_state=42))],\n",
       "                                                 final_estimator=LinearRegression()))</pre></div> </div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" ><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">estimator: StackingRegressor</label><div class=\"sk-toggleable__content fitted\"><pre>StackingRegressor(estimators=[(&#x27;ridge&#x27;, Ridge()),\n",
       "                              (&#x27;rf&#x27;, RandomForestRegressor(random_state=42)),\n",
       "                              (&#x27;adaboost&#x27;, AdaBoostRegressor(random_state=42))],\n",
       "                  final_estimator=LinearRegression())</pre></div> </div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><label>ridge</label></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" ><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;Ridge<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.5/modules/generated/sklearn.linear_model.Ridge.html\">?<span>Documentation for Ridge</span></a></label><div class=\"sk-toggleable__content fitted\"><pre>Ridge()</pre></div> </div></div></div></div></div><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><label>rf</label></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-4\" type=\"checkbox\" ><label for=\"sk-estimator-id-4\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;RandomForestRegressor<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.5/modules/generated/sklearn.ensemble.RandomForestRegressor.html\">?<span>Documentation for RandomForestRegressor</span></a></label><div class=\"sk-toggleable__content fitted\"><pre>RandomForestRegressor(random_state=42)</pre></div> </div></div></div></div></div><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><label>adaboost</label></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-5\" type=\"checkbox\" ><label for=\"sk-estimator-id-5\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;AdaBoostRegressor<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.5/modules/generated/sklearn.ensemble.AdaBoostRegressor.html\">?<span>Documentation for AdaBoostRegressor</span></a></label><div class=\"sk-toggleable__content fitted\"><pre>AdaBoostRegressor(random_state=42)</pre></div> </div></div></div></div></div></div></div><div class=\"sk-item\"><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><label>final_estimator</label></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-6\" type=\"checkbox\" ><label for=\"sk-estimator-id-6\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;LinearRegression<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.5/modules/generated/sklearn.linear_model.LinearRegression.html\">?<span>Documentation for LinearRegression</span></a></label><div class=\"sk-toggleable__content fitted\"><pre>LinearRegression()</pre></div> </div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>"
      ],
      "text/plain": [
       "MultiOutputRegressor(estimator=StackingRegressor(estimators=[('ridge', Ridge()),\n",
       "                                                             ('rf',\n",
       "                                                              RandomForestRegressor(random_state=42)),\n",
       "                                                             ('adaboost',\n",
       "                                                              AdaBoostRegressor(random_state=42))],\n",
       "                                                 final_estimator=LinearRegression()))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型训练\n",
    "multi_output_model.fit(X_train, Y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7817c45e23e1a1a",
   "metadata": {},
   "source": [
    "### step8：模型评估\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6aa90236749de9ff",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:15:56.581426Z",
     "start_time": "2024-12-30T11:15:56.564468Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MSE: 1.7812606954451455e-06\n",
      "R2: 0.9994718137366516\n",
      "MAE: 0.0002150470201706066\n",
      "RMSE: 0.0013346387883787678\n"
     ]
    }
   ],
   "source": [
    "Y_pred = multi_output_model.predict(X_test)\n",
    "# 计算MSE\n",
    "mse = mean_squared_error(Y_test, Y_pred)\n",
    "print(f\"MSE: {mse}\")\n",
    "# 计算R2\n",
    "r2 = r2_score(Y_test, Y_pred)\n",
    "print(f\"R2: {r2}\")\n",
    "# 计算MAE\n",
    "mae = mean_absolute_error(Y_test, Y_pred)\n",
    "print(f\"MAE: {mae}\")\n",
    "# 计算RMSE\n",
    "rmse = math.sqrt(mse)\n",
    "print(f\"RMSE: {rmse}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef925867",
   "metadata": {},
   "source": [
    "### step9：模型保存\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fc367776",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved as ONNX format!\n"
     ]
    }
   ],
   "source": [
    "import skl2onnx\n",
    "from skl2onnx import convert_sklearn\n",
    "from skl2onnx.common.data_types import FloatTensorType\n",
    "import onnx\n",
    "\n",
    "# 将模型转换为 ONNX 格式\n",
    "onnx_model = convert_sklearn(multi_output_model, initial_types=[('input', FloatTensorType([None, X.shape[1]]))])\n",
    "\n",
    "# 保存 ONNX 模型到文件\n",
    "onnx.save_model(onnx_model, 'model.onnx')\n",
    "\n",
    "print(\"Model saved as ONNX format!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3dfa787",
   "metadata": {},
   "source": [
    "### step10：模型加载\n",
    "**请勿修改此部分**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8e74572b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ONNX model is valid.\n"
     ]
    }
   ],
   "source": [
    "import onnx\n",
    "\n",
    "# 加载导出的 ONNX 模型\n",
    "onnx_model = onnx.load(\"model.onnx\")\n",
    "\n",
    "# 验证模型\n",
    "onnx.checker.check_model(onnx_model)\n",
    "\n",
    "# 如果没有异常抛出，说明模型是有效的\n",
    "print(\"ONNX model is valid.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "883109b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inference result: [array([[ 2.5052875e-03,  1.6185946e-06,  5.7117114e-07,  1.9258831e-01,\n",
      "         1.7092326e-01,  2.1180388e-01,  1.8772888e-01,  1.8393432e-01,\n",
      "         1.9547628e-01,  2.4072981e-01,  2.0911123e-01,  3.7783951e-01,\n",
      "         3.3117274e-03,  8.4127432e-01, -7.8964722e-06,  9.4649522e-03,\n",
      "         6.4186960e-01]], dtype=float32)]\n"
     ]
    }
   ],
   "source": [
    "import onnxruntime as ort\n",
    "import numpy as np\n",
    "\n",
    "# 加载 ONNX 模型\n",
    "onnx_session = ort.InferenceSession(\"model.onnx\")\n",
    "\n",
    "# 准备输入数据\n",
    "# input_data = X.astype(np.float32)  # 示例数据，形状应与训练时输入数据一致\n",
    "input_data = np.array([[0.0005375, 0.4387, 0.1929875]]).astype(np.float32)\n",
    "\n",
    "# 获取模型的输入和输出名称\n",
    "input_name = onnx_session.get_inputs()[0].name\n",
    "output_name = onnx_session.get_outputs()[0].name\n",
    "\n",
    "# 进行推理\n",
    "result = onnx_session.run([output_name], {input_name: input_data})\n",
    "\n",
    "# 打印结果\n",
    "print(\"Inference result:\", result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
